/**
*      author: Y_Yao
*      created: 2025-04-21 10:57:40
**/

#include<bits/stdc++.h>

#define int LL
using namespace std;
using LL = long long;
const LL LLINF = 9e18;
const int INF = 0x3f3f3f3f;
int n,m;
int dp[1050];
int a[100 + 7][2];

void bfs()
{
    int x,step;
    queue<pair<int,int>> q;
    q.push({pow(2,n) - 1,0});

    while(!q.empty())
    {
        auto t = q.front();
        q.pop();
        x = t.first,step = t.second;
        if(dp[x]) continue;
        dp[x] = step;
        if(x == 0) return;
        for(int i = 1;i <= m;i++)
        {
            q.push({x & a[i][0] | a[i][1],step + 1});
        }
    }
}

void solve()
{
   cin >> n >> m;
   for(int i = 1;i <= m;i++)
   {
        int s1 = 0,s2 = 0;
        for(int j = 1;j <= n;j++)
        {
            int x;
            cin >> x;
            if(x == 0)
            {
                s1 = s1 * 2 + 1;
                s2 = s2 * 2 + 0;
            }
            else if(x == 1)
            {
                s1 = s1 * 2 + 0;
                s2 = s2 * 2 + 0;
            }
            else
            {
                s1 = s1 * 2 + 1;
                s2 = s2 * 2 + 1;
            }
        }
        a[i][0] = s1,a[i][1] = s2;
   }

   bfs();
   if(dp[0]) cout << dp[0] << endl;
   else cout << -1 << endl;
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int t = 1;
    // cin >> t;
    while(t--)
    {
        solve();
    }
    return 0;
}